<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Rent;
use App\Models\RentLog;

class RentLogController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $param  = [];
        $data   = [];
        $query  = RentLog::query();
        if($request->rent_id) {
            $rent_id = trim($request->rent_id);
            $param['rent_id'] = $rent_id;
            $query->where('rent_id','=',$rent_id);
            $data = Rent::find($rent_id);
        }
        $list = $query->orderBy('created_at','desc')->paginate(20);

        return view('admin.rent-log.index',compact('list','param','data'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.rent-log.add');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $rs['msg']    = '操作成功';
        $rs['status'] = 'success';
        $data = $request->except('_token','rent_status');
        RentLog::create($data);
        Rent::where('id',$data['rent_id'])->update(['status'=>$request->rent_status]);
        return back()->with('rs',$rs);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {

        $data = RentLog::find($id);
        $rent = Rent::find($data->rent_id);
        return view('admin.rent-log.edit',compact('data','rent'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $rs['msg']    = '操作成功';
        $rs['status'] = 'success';
        $data = $request->except('_token','_method','rent_status');
        RentLog::where('id',$id)->update($data);
        
        Rent::where('id',$data['rent_id'])->update(['status'=>$request->rent_status]);
        return back()->with('rs',$rs);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $rs['msg']    = '操作成功';
        $rs['status'] = 'success';
        RentLog::destroy($id);
        return back()->with('rs',$rs);

    }
}
